package com.example.yz.happywoker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVGeoPoint;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.example.yz.happywoker.HistoryOrderActivity.MyAdapter;
import com.example.yz.happywoker.listViewActivityRelevant.MyListViewAdapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.yz.happywoker.R.id.parent;

public class ListViewActivity extends Activity implements View.OnClickListener{
    public ListView MyListView;
    public MyListViewAdapter MyAdapter;
    private ImageView suggestion_listview03;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        MyListView = (ListView) findViewById(R.id.MyList);
        suggestion_listview03 = (ImageView) findViewById(R.id.suggestion_listview);
        suggestion_listview03.setOnClickListener(this);
        GlobalData.mylistview.clear();
        if (isNetworkAvailable(this)) {
            AVQuery<AVObject> query = new AVQuery<>("MyOrderList");
            double lat = getIntent().getDoubleExtra("lat",39.9260173);
            double log = getIntent().getDoubleExtra("log",116.6563895);
            AVGeoPoint point = new AVGeoPoint(lat, log);
            query.limit(50);
            query.whereNear("Location", point);
            query.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> list, AVException e) {
                for (AVObject avObject : list) {
                    if (avObject.get("Status").equals("1")) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("RepairCategory", avObject.getString("RepairCategory"));
                        map.put("Status", "急招");
                        map.put("HandleTime", ConverToString(avObject.getDate("createdAt")));
                        map.put("ServiceContent", avObject.getString("ServiceContent"));
                        map.put("RealAddress", avObject.getString("RealAddress"));
                        map.put("objectid", avObject.getObjectId());
                        GlobalData.mylistview.add(map);
                        MyAdapter = new MyListViewAdapter(ListViewActivity.this, GlobalData.mylistview);
                        MyListView.setAdapter(MyAdapter);
                        MyListView.setOnItemClickListener(new AdapterView.OnItemClickListener()//监听后放在setAdapter后面

                        {
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            if (isNetworkAvailable(ListViewActivity.this)) {
                                Intent intent = new Intent();
                                intent.putExtra("myobject", GlobalData.mylistview.get(position).get("objectid").toString());
                                intent.setClass(ListViewActivity.this, ListDetailActivity.class);
                                startActivity(intent);
                            } else {
                                Toast.makeText(ListViewActivity.this, "当前无网络连接", Toast.LENGTH_SHORT).show();
                            }
                            }


                        });
                    }
                }
                }
            });

        } else {
            Toast.makeText(this, "当前无网络连接", Toast.LENGTH_SHORT).show();
        }
    }

    //把日期转为字符串
    public String ConverToString(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(date);
    }

    private boolean isNetworkAvailable(Activity activity) {
        Context context = activity.getApplicationContext();
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        } else {
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.suggestion_listview:
                finish();
                break;
        }
    }
}